#pragma once

#include <Windows.h>
#include "NuiApi.h"

class AnimNui
{
	bool                    m_fUpdatingUi;
	INuiSensor*             m_pNuiSensor;
	BSTR                    m_instanceId;      
	HRESULT hr;	

	int tick;
	
	static DWORD WINAPI     Nui_ProcessThread(LPVOID pParam);

	// thread handling
	HANDLE        m_hThNuiProcess;
	HANDLE        m_hEvNuiProcessStop;

	HANDLE        m_hNextDepthFrameEvent;
	HANDLE        m_hNextColorFrameEvent;
	HANDLE        m_hNextSkeletonEvent;
	HANDLE        m_pDepthStreamHandle;
	HANDLE        m_pVideoStreamHandle;
	DWORD         m_LastSkeletonFoundTime;
	POINT         m_Points[NUI_SKELETON_POSITION_COUNT];
	DWORD         m_SkeletonIds[NUI_SKELETON_COUNT];
	DWORD         m_TrackedSkeletonIds[NUI_SKELETON_MAX_TRACKED_COUNT];
	ULONG_PTR     m_GdiplusToken;
public:	
	AnimNui(void);
	~AnimNui(void);

	DWORD WINAPI            Nui_ProcessThread();
	void Nui_Zero();
	HRESULT Initialize();
	void DeInitialize();

	void Nui_GotSkeletonAlert();
	void Nui_GotColorAlert( );
	void Nui_GotDepthAlert( );

	void CALLBACK Nui_StatusProcThunk( HRESULT hrStatus, const OLECHAR* instanceName, const OLECHAR* uniqueDeviceName, void * pUserData );
	void CALLBACK Nui_StatusProc( HRESULT hrStatus, const OLECHAR* instanceName, const OLECHAR* uniqueDeviceName );

	NUI_SKELETON_DATA* currentSkeleton[NUI_SKELETON_COUNT];
};

static const COLORREF g_JointColorTable[NUI_SKELETON_POSITION_COUNT] = 
{
	RGB(169, 176, 155), // NUI_SKELETON_POSITION_HIP_CENTER
	RGB(169, 176, 155), // NUI_SKELETON_POSITION_SPINE
	RGB(168, 230, 29),  // NUI_SKELETON_POSITION_SHOULDER_CENTER
	RGB(200, 0,   0),   // NUI_SKELETON_POSITION_HEAD
	RGB(79,  84,  33),  // NUI_SKELETON_POSITION_SHOULDER_LEFT
	RGB(84,  33,  42),  // NUI_SKELETON_POSITION_ELBOW_LEFT
	RGB(255, 126, 0),   // NUI_SKELETON_POSITION_WRIST_LEFT
	RGB(215,  86, 0),   // NUI_SKELETON_POSITION_HAND_LEFT
	RGB(33,  79,  84),  // NUI_SKELETON_POSITION_SHOULDER_RIGHT
	RGB(33,  33,  84),  // NUI_SKELETON_POSITION_ELBOW_RIGHT
	RGB(77,  109, 243), // NUI_SKELETON_POSITION_WRIST_RIGHT
	RGB(37,   69, 243), // NUI_SKELETON_POSITION_HAND_RIGHT
	RGB(77,  109, 243), // NUI_SKELETON_POSITION_HIP_LEFT
	RGB(69,  33,  84),  // NUI_SKELETON_POSITION_KNEE_LEFT
	RGB(229, 170, 122), // NUI_SKELETON_POSITION_ANKLE_LEFT
	RGB(255, 126, 0),   // NUI_SKELETON_POSITION_FOOT_LEFT
	RGB(181, 165, 213), // NUI_SKELETON_POSITION_HIP_RIGHT
	RGB(71, 222,  76),  // NUI_SKELETON_POSITION_KNEE_RIGHT
	RGB(245, 228, 156), // NUI_SKELETON_POSITION_ANKLE_RIGHT
	RGB(77,  109, 243)  // NUI_SKELETON_POSITION_FOOT_RIGHT
};

static const COLORREF g_SkeletonColors[NUI_SKELETON_COUNT] =
{
	RGB( 255, 0, 0),
	RGB( 0, 255, 0 ),
	RGB( 64, 255, 255 ),
	RGB( 255, 255, 64 ),
	RGB( 255, 64, 255 ),
	RGB( 128, 128, 255 )
};

//lookups for color tinting based on player index
static const int g_IntensityShiftByPlayerR[] = { 1, 2, 0, 2, 0, 0, 2, 0 };
static const int g_IntensityShiftByPlayerG[] = { 1, 2, 2, 0, 2, 0, 0, 1 };
static const int g_IntensityShiftByPlayerB[] = { 1, 0, 2, 2, 0, 2, 0, 2 };
